var ____debug_mode = false;
if (document.location.href.indexOf('debug=true') > -1) {
	____debug_mode = true;
	if (console && console.log) {
		window.dlog = function(message) {
			if (typeof message == 'string'){
				console.log('FT: ' + message);
			}else{
				console.log('FT: ');
				console.log(message);
			}
			$('#__dloger').append('<p>' + message + '</p>');
		};
	}else{
		$(document.body).append('<p>' + message + '</p>');
	}

	if (document.location.href.indexOf('dlog=doc') > -1) {
		$(function (){
			$('<div id="__dloger" style="font-size:28px;color:#000;background-color:#FFF;border-bottom:1px solid #F00;margin-bottom:10px;"></div>').insertBefore($('> *', document.body).eq(0))
		})
	}
}else{
	window.dlog = function (message){};
}
// Hide default menu bar
var toolbarWhiteList = ['/mobile/service/'],
	showToolbar = false,
	pathname = document.location.pathname.replace(/^\/index\.php/i, '');
for (var i = 0; i < toolbarWhiteList.length; i++) {
	if (pathname.indexOf(toolbarWhiteList[i]) == 0) {
		showToolbar = true;
	}
}
// Share button White list
var whitelist = ['/mobile/products', '/mobile/fotile-world', '/mobile/fotile/world', '/mobile/service/', '/one/chat', '/mobile/srv-test/', '/ft-con/'],
	showOptionMenu = false;
for (var i = 0; i < whitelist.length; i++) {
	if (pathname.indexOf(whitelist[i]) == 0) {
		showOptionMenu = true;
	}
}

document.addEventListener('WeixinJSBridgeReady', function onBridgeReady() {
	WeixinJSBridge.call( showToolbar ? 'showToolbar' : 'hideToolbar' );
	WeixinJSBridge.call( showOptionMenu ? 'showOptionMenu' : 'hideOptionMenu' );
});

// // Just font API
var _jf = _jf || [];
_jf.push(['_setAppId', '9c641a44AJV7tO2EKPv4XnwzTJzVr0jQLn6vC8S4cl39lfMALIGAExjL7keSztWMoj7me8AcU4C42cVU8APBzJs0WNk5u56x-Hf6ZVyafBXHBiJSBxfelTvATuXAvGPpY8n78BaRjmkm9oPCp2x5Rzk5jBYozjei2FuhWK8g448LSyQNfhc=']);
_jf.push(['p', '26539']);
_jf.push(['_setFont', 'daty3', 'css', '.daty3']);
_jf.push(['_setFont', 'daty3', 'alias', 'daty3']);
_jf.push(['_setFont', 'gslh00l', 'css', '.font-fotile']);
_jf.push(['_setFont', 'gslh00l', 'alias', 'font-fotile']);
(function(f, q, c, h, e, i, r, d) {var k = f._jf; if (k.constructor === Object) {return } var l, t = q.getElementsByTagName("html")[0], a = function(u) {for (var v in k) {if (k[v][0] == u) {if (false === k[v][1].call(k)) {break } } } }, j = /\S+/g, o = /[\t\r\n\f]/g, b = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, g = "".trim, s = g && !g.call("\uFEFF\xA0") ? function(u) {return u == null ? "" : g.call(u) } : function(u) {return u == null ? "" : (u + "").replace(b, "") }, m = function(y) {var w, z, v, u, x = typeof y === "string" && y; if (x) {w = (y || "").match(j) || []; z = t[c] ? (" " + t[c] + " ").replace(o, " ") : " "; if (z) {u = 0; while ((v = w[u++])) {if (z.indexOf(" " + v + " ") < 0) {z += v + " "} } t[c] = s(z) } } }, p = function(y) {var w, z, v, u, x = arguments.length === 0 || typeof y === "string" && y; if (x) {w = (y || "").match(j) || []; z = t[c] ? (" " + t[c] + " ").replace(o, " ") : ""; if (z) {u = 0; while ((v = w[u++])) {while (z.indexOf(" " + v + " ") >= 0) {z = z.replace(" " + v + " ", " ") } } t[c] = y ? s(z) : ""} } }, n; k.push(["_eventActived", function() {p(h); m(e) } ]); k.push(["_eventInactived", function() {p(h); m(i) } ]); k.addScript = n = function(u, A, w, C, E, B) {E = E || function() {}; B = B || function() {}; var x = q.createElement("script"), z = q.getElementsByTagName("script")[0], v, y = false, D = function() {x.src = ""; x.onerror = x.onload = x.onreadystatechange = null; x.parentNode.removeChild(x); x = null; a("_eventInactived"); B() }; if (C) {v = setTimeout(function() {D() }, C) } x.type = A || "text/javascript"; x.async = w; x.onload = x.onreadystatechange = function(G, F) {if (!y && (!x.readyState || /loaded|complete/.test(x.readyState))) {y = true; if (C) {clearTimeout(v) } x.src = ""; x.onerror = x.onload = x.onreadystatechange = null; x.parentNode.removeChild(x); x = null; if (!F) {setTimeout(function() {E() }, 200) } } }; x.onerror = function(H, G, F) {if (C) {clearTimeout(v) } D(); return true }; x.src = u; z.parentNode.insertBefore(x, z) }; a("_eventPreload"); m(h); n(r, "text/javascript", false, 3000) })(this, this.document, "className", "jf-loading", "jf-active", "jf-inactive", "//ds.justfont.com/js/stable/v/2.6/id/109865513640");

var _enableDebug = false;
function log(msg){
	if(!_enableDebug) return;

	// alert(msg);
	console.log(msg);
}

function getURLParameter(url) {
	var urlStr = url || window.location.search,
		param = {};

	if (urlStr) {
		var urlArr = urlStr.indexOf('?') == 0 ? urlStr.split("?")[1].split("&") : urlStr.split("&");

		for (var i = urlArr.length - 1; i >= 0; i--) {
			var tempArr = urlArr[i].split("=");
			param[tempArr[0]] = decodeURIComponent(tempArr[1]);
		}
	}
	return param;
}

function serializeObject(obj) {
	var _str = [];
	for (var _t in obj) {
		_str.push(_t + '=' + encodeURIComponent(obj[_t]));
	}
	return _str.join('&');
}

(function($) {
	if (document._FTW) return;
	var FTW = {
		urlHome: ''
	};
	// Find home url

	//
	FTW.refresh = function() {
		document.location.reload();
	};
	FTW.goBack = function() {
		window.history.back(-1);
	};
	FTW.goHome = function() {
		document.location = FTW.urlHome;
		return false;
	};


	document._FTW = document.FTW = window.FTW = FTW;
	return FTW;
})(jQuery);

var browser = {
	versions: function() {
		var u = navigator.userAgent,
			app = navigator.appVersion;
		return { //移动终端浏览器版本信息
			trident: u.indexOf('Trident') > -1, //IE内核
			presto: u.indexOf('Presto') > -1, //opera内核
			webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核
			gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核
			mobile: !! u.match(/AppleWebKit.*Mobile/) || !! u.match(/Windows Phone/) || !! u.match(/Android/) || !! u.match(/MQQBrowser/), //是否为移动终端
			ios: !! u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
			android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或者uc浏览器
			iPhone: u.indexOf('iPhone') > -1 || u.indexOf('Mac') > -1, //是否为iPhone或者QQHD浏览器
			iPad: u.indexOf('iPad') > -1, //是否iPad
			webApp: u.indexOf('Safari') == -1, //是否web应该程序，没有头部与底部
			msie: u.indexOf('MSIE') > -1
		};
	}(),
	language: (navigator.browserLanguage || navigator.language).toLowerCase()
};


$(function() {

	if (browser.versions.ios) {
		if ($(document.body).css('background-image') != 'none') {
			$(document.body).append('<div id="FIXEDBG">&nbsp;</div>');
			$('>*', document.body).not('#mainnav, #FIXEDBG, .nofix').css({
				'position': 'relative',
				'z-index': 50
			});
		}
	}

	if (browser.versions.ios) {
		$('html').addClass('ios');
		dlog('platform: ios');
	}else if (browser.versions.android) {
		$('html').addClass('android');
		dlog('platform: android');
	}else if (browser.versions.winphone) {
		$('html').addClass('winphone');
		dlog('platform: winphone');
	}else if(browser.versions.msie){
		$('html').addClass('msie');
		dlog('platform: msie');
	}

	var devicePR = 1.5;
	if (window.devicePixelRatio) {
		dlog('devicePixelRatio = ' + window.devicePixelRatio);
		devicePR = window.devicePixelRatio;
	}else{
		dlog('devicePixelRatio unsurpport');
	}
	

	var viewport = $('meta[name="viewport"]'),
		VPwhiteList = ['/mobile/fotile-world/no-scale','/mobile/fotile/world/','/one/chat','/mobile/assist','/mobile/products','/mobile/newpro'],
		inWhiteList = false,
		VPBlackList = ['/mobile/fotile/world/latest/news', '/mobile/fotile/world/brand/news'],
		inBlackList = false;
	var path = document.location.pathname,
		adjust = false;

	for(var i=0;i<VPBlackList.length;i++){
		if(path.indexOf(VPBlackList[i]) > -1){
			inBlackList = true;
			break;
		}
	}
	for(var i=0;i<VPwhiteList.length;i++){
		if(path.indexOf(VPwhiteList[i]) > -1){
			inWhiteList = true;
			dlog('page ['+VPwhiteList[i]+']\'s viewport need adjust');
			break;
		}
	}
	adjust = !inBlackList && inWhiteList;
	// alert($(window).width());
	if (viewport.length > 0 && adjust) {
		if (viewport.data('ignore') == 'true') return;

		dlog('original viewport: ' + viewport.attr('content'));
		//
		// Fix viewport scale problem
		//
		var w = $(window).width(),
		sw = w / 640 * (path.indexOf('/one/chat') > -1 ? 1 : 0.5);
		// sw=sw*(devicePR/1.5);
		dlog('device window width: ' + w + ' > ' + sw);

		var vp_content = 'width=device-width, initial-scale=' + sw + ', minimum-scale=' + sw + ',maximum-scale=' + sw + ',target-densitydpi=device-dpi';
		viewport.replaceWith('<meta name="viewport" content="' + vp_content + '" >');
		dlog('modified viewport: ' + $('meta[name="viewport"]').attr('content'));
		dlog('device window width: ' + w + ' > ' + sw);
	} else {
		if (browser.versions.android && document.location.pathname.indexOf('/mobile/service') == -1) {
			if ($(document.body).width() <= 320) {
				// 是不是要计算缩放比例?
				$(document.body).append('<style type="text/css">body{zoom:0.5;}</style>');
				dlog('force scale document.body to 0.5 to fix low reslotion screen of android device');
			}
		}
	}

	if (String($(document.body).data('index')) == 'true') {
		// Set home page
		$.cookie('homepage', document.location.href, {
			path: '/'
		});
		// console.log('log home url: ' + document.location.href);
	}
	FTW.urlHome = $.cookie('homepage');
	// console.log('get current home: ' + FTW.urlHome);

});

//
 (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-48220472-1', 'fotile.com');
  ga('send', 'pageview');